//
//  DHS2_SearchView_Community.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView_Community.h"
#import "DHCommunity_NoImageCell.h"
#import "DHCommunityDetailController.h"

@interface DHS2_SearchView_Community()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *communityArr;

@end

@implementation DHS2_SearchView_Community

+ (DHS2_SearchView_Community *)shareDHS2_SearchView_Community{
    DHS2_SearchView_Community *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView_Community class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCommunity_NoImageCell" bundle:nil] forCellReuseIdentifier:kDHCommunity_NoImageCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

- (NSMutableArray *)communityArr{
    if (!_communityArr) {
        _communityArr = [NSMutableArray array];
    }
    return _communityArr;
}

//搜索
- (void)searchData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"社区" forKey:@"type"];
    [params setObject:self.keyWord forKey:@"key"];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    [[DHNetAPIClient shareDHNetAPIClient] request_app_searchWithParams:params AndBlock:^(id data, NSError *error) {
        if (data) {
            [self.tableView.mj_header endRefreshing];
            
            [self.communityArr removeAllObjects];
            
            NSDictionary *dic = [data objectForKey:@"results"];
            [self.communityArr addObjectsFromArray:[dic objectForKey:@"posts"]];
            
            if (self.communityArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
        }
    }];
}

- (void)searchMoreData{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.communityArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.communityArr objectAtIndex:indexPath.row];
    NSArray *images = [model objectForKey:@"images"];
    
    //计算文字高度
    CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
    //如果内容中有 \n 等转义字符
    NSInteger specialChar = 0;//出现次数
    specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
    CGFloat textHeight = contentHeight + specialChar * 14;
    
    //图片CollectionView高度
    CGFloat imgsHeight = 0;
    if (images.count == 0) {
        imgsHeight = 0;
    }else if (images.count > 0 && images.count <= 3){
        imgsHeight = 128;
    }else if (images.count > 3 && images.count <= 6){
        imgsHeight = 128 * 2 + 5;
    }else if (images.count > 6 && images.count <= 9){
        imgsHeight = 128 * 3 + 10;
    }
    
    return imgsHeight + textHeight + 133;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = self.communityArr[indexPath.row];
    DHCommunity_NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCommunity_NoImageCell forIndexPath:indexPath];
    [cell updateCell:[self.communityArr objectAtIndex:indexPath.row]];
    cell.row = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.communityArr objectAtIndex:indexPath.row];
    DHCommunityDetailController *vc = [[DHCommunityDetailController alloc] initWithCommunityDic:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

@end
